<?php declare(strict_types=1);


namespace App\Rpc\Traits\Service;


use App\Lib\Error\ErrorConstant;
use App\Lib\Error\Response;
use App\Rpc\Model\Dao\UserDao;
use App\Rpc\Model\Dao\UserDriverDao;
use App\Rpc\Model\Logic\PermissionLogic;
use Dotenv\Exception\ValidationException;
use Swoft\Bean\BeanFactory;
use Swoft\Validator\Exception\ValidatorException;

/**
 * Trait UserDriverTrait
 * @package App\Rpc\Traits\Service
 */
trait UserDriverTrait
{
    /************************************************司机部分**************************************************************/
    /**
     * 司机注册
     * @param array $data
     * @return array
     */
    public function userDriverCreate(array $data): array
    {
        try{
            $account = $data['account']??null;
            $access_token = $data['access_token']??null;
            if(!$account || !$access_token){
                Response::FatalError(ErrorConstant::VALIDATE_ERROR_CODE, '请求参数错误');
            }
            /** @var PermissionLogic $PermissionLogic */
            $PermissionLogic = BeanFactory::getBean(PermissionLogic::class);
            $result = $PermissionLogic->userDriverCreate($data);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 用户更新
     * @param array $data
     * @return array
     */
    public function userDriverUpdate(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            /** @var UserDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDao::class);
            $result = $UserDao->update($id, $data);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 用户删除
     * @param array $data
     * @return array
     */
    public function userDriverDelete(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);
            $flag = $data['flag']??false;
            /** @var UserDriverDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDriverDao::class);
            $result = $UserDao->delete($id, $flag);
            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 账号信息查询
     * @param array $data
     * @return array
     */
    public function userDriverInfo(array $data): array
    {
        try{
            //添加item_id
            //根据item_id查询
            $id = $data['item_id']??null;
            unset($data['item_id']);
            $where['item_id'] = $id;
            /** @var UserDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDao::class);
            $result = $UserDao->getByWhere($where);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 后台注册司机默认密码为123456
     * @param array $data
     * @return array
     */
    public function registerDriver(array $data): array {
        try {
            //验证区域
            $required = [
                'user_item_id' => '请输入user_item_id',
                'identity_id' => '请输入身份id',
                'tenant' => '请输入租户信息',
                'account' => '请输入注册手机号',
            ];
            foreach ($required as $key => $require) {
                if(!isset($data[$key]) || $data[$key] === ''){
                    throw new ValidatorException($require, 4001);
                }
            }
            if($data['identity_id'] != 2){
                throw new ValidatorException('登陆账号无权限', 4002);
            }
            //实现区域
            $data['password'] = $data['password']??'123456';

            //@2同时验证数据
            /** @var UserDriverDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDriverDao::class);
            //验证数据是否合理更新
            $diyValidator = [
                'UserDriverValidator' => [ 'op' => 'add']
            ];
            //创建盐匙
            $salt = create_salt();
            $validData = [
                'item_id' => create_item_id(uuid()),
                'account' => $data['account'],
                'password' => create_password($data['password'], $salt),
                'salt'   => $salt,
                'frozen_state' => 0,
            ];

            validate($validData, "UserDriverValidator", [], $diyValidator);
            //@3提交到司机用户表
            $result = $UserDao->insert($validData);
            if(!$result){
                throw new ValidationException('司机注册失败', 4003);
            }
            //@4司机注册成功
            //返回区域
            return Response::Info(['item_id' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }
    /************************************************end司机部分***********************************************************/

}
